package windowchat;

import guiclient.Application;
import guicommon.controls.*;
import guicommon.controls.handlers.*;
import java.awt.Color;
import java.io.IOException;

public class ChatWindow extends Application{

    private Window window;
    private Button clearButton, sendButton;
    private TextBox msgTextBox;
    private Label idLabel, chatLabel, sendLabel;
    private TextArea chatTextArea;

    private String id;

    public ChatWindow(int x, int y) throws IOException {
        window = new Window();
        window.SetSize(300, 250);
        window.SetPoint(x, y);
        window.SetTitle("Chat");
        window.SetBackGround(Color.lightGray);
        RegisterWindow(window);

        idLabel = new Label();
        idLabel.SetSize(180, 20);
        idLabel.SetPoint(35, 10);
        idLabel.SetOwner(window);
        idLabel.SetText("My text area server id: ");
        RegisterControl(idLabel);

        chatLabel = new Label();
        chatLabel.SetSize(180, 20);
        chatLabel.SetPoint(35, 35);
        chatLabel.SetOwner(window);
        chatLabel.SetText("Chat log: ");
        RegisterControl(chatLabel);

        chatTextArea = new TextArea();
        chatTextArea.SetSize(230, 60);
        chatTextArea.SetPoint(35, 60);
        chatTextArea.SetOwner(window);
        chatTextArea.SetText("");
        chatTextArea.SetBackGround(Color.white);
        chatTextArea.SetIdChangedEventHandler(new IdChangedEventHandler() {

            @Override
            public void Event(Control sender, IdEventArgs args) {
               id = args.GetId();
               idLabel.SetText(idLabel.GetText() + id);
            }
        });
        RegisterControl(chatTextArea);

        sendLabel = new Label();
        sendLabel.SetSize(180, 20);
        sendLabel.SetPoint(35, 125);
        sendLabel.SetOwner(window);
        sendLabel.SetText("Send form: ");
        RegisterControl(sendLabel);

        msgTextBox = new TextBox();
        msgTextBox.SetSize(230, 20);
        msgTextBox.SetPoint(35, 150);
        msgTextBox.SetOwner(window);
        msgTextBox.SetText("");
        msgTextBox.SetBackGround(Color.white);
        msgTextBox.SetKeyPressedEventHandler(new KeyPressedEventHandler() {

            @Override
            public void Event(Control sender, KeyEventArgs args) {
                if (args.IsPressed()) {
                    String text = msgTextBox.GetText();
                    String symbol = args.GetSymbol();
                    if ("Backspace".equals(symbol)) {
                        int length = text.length();
                        if (length > 0) {
                            text = text.substring(0, length - 1);
                        }
                    } else {
                        text = text + args.GetSymbol();
                    }
                    msgTextBox.SetText(text);
                }
            }
        });
        RegisterControl(msgTextBox);

        clearButton = new Button();
        clearButton.SetSize(80, 20);
        clearButton.SetPoint(50, 185);
        clearButton.SetOwner(window);
        clearButton.SetBackGround(Color.red);
        clearButton.SetText("Clear text");
        clearButton.SetMouseClickedEventHandler(new MouseClickedEventHandler() {

            @Override
            public void Event(Control sender, MouseEventArgs args) {
                msgTextBox.SetText("");
            }
        });
        RegisterControl(clearButton);

        sendButton = new Button();
        sendButton.SetSize(110, 20);
        sendButton.SetPoint(140, 185);
        sendButton.SetOwner(window);
        sendButton.SetBackGround(Color.green);
        sendButton.SetText("Send message");
        sendButton.SetMouseClickedEventHandler(new MouseClickedEventHandler() {

            @Override
            public void Event(Control sender, MouseEventArgs args) {
                String text = msgTextBox.GetText();
                String[] splitted = text.split(":");
                if (splitted.length == 2) {
                    SendUpdateMessage(splitted[0], "AppendText", String.format("Message from %s: %s", id, splitted[1]));
                }
            }
        });
        RegisterControl(sendButton);
    }

    public void SetPoint(int x, int y) {
        window.SetPoint(x, y);
    }
}
